package cn.wit.db.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

// java配置类必须的注解
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
	// @Value("${cors.allowed.hosts}")
	// String[] allowedHosts;
	
	// @Value("${file.access.path}")
	// String fileAccessPath;
	
	// @Value("${file.save.path}")
	// String fileSavePath;

	/**
	 * 配置跨域访问
	 * */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		// System.out.println("allowedHosts:" + Arrays.toString(allowedHosts));
		registry.addMapping("/**")
			// .allowedOrigins("http://localhost:9527", "http://127.0.0.1:9527")
			// .allowedOrigins(allowedHosts)
			// https://stackoverflow.com/questions/37897523/axios-get-access-to-response-header-fields
			.exposedHeaders("Authorization")
			.allowedMethods("DELETE", "POST", "PUT", "GET", "");
	}
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		 registry.addViewController("/").setViewName("/index.html");
	}
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/upload/"); 
		//registry.addResourceHandler(fileAccessPath + "**").addResourceLocations("file:" + fileSavePath);
		
//		registry.addResourceHandler("/img/").addResourceLocations("/static/img/**");
		// registry.addResourceHandler("/dist/index.html").addResourceLocations("/");
	}
}
